package com.yeswater.multithread.chapter1.test1_11.test5;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * author : yeswater
 * date : 2023/10/19
 *
 * java.lang.InterruptedException: sleep interrupted
 * interrupter 和 sleep 遇到就会出现异常
 */

public class RunTest {

    /**
     * 线程在 sleep 状态下停止,则该线程会进入 catch 语句,并且清空停止状态值,变成 false
     */
    @Test
    public void test1() {
        try {
            MyThread t1 = new MyThread();
            t1.start();
            TimeUnit.MILLISECONDS.sleep(200);
            t1.interrupt();
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("end !");
    }

    /**
     * interrupter 之后再 sleep 也会进入 catch
     */
    @Test
    public void test2() {
        MyThread1 t1 = new MyThread1();
        t1.start();
        t1.interrupt();
        System.out.println("end!");
    }

}
